import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/components/Login"
import Home from "@/components/Home";
import UserView from "@/views/UserView";
import GoodsView from "@/views/GoodsView";

Vue.use(VueRouter)

const routes = [
  // {
  //   path: '/',
  //   name: 'home',
  //   component: HomeView
  // }
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect:'/home/user_list',//重定向,让用户登录以后直接进入home的子路由user_list的页面
    children:[
      {
        path:'user_list',component:UserView
      },
      {
        path:'pro_list',component:GoodsView
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path==='/login'){
    return next()
  }else {
    let token = window.localStorage.getItem("token")
    if (token){
      return next();
    }else {
      Vue.prototype.$message.error("请先登录")
      return next('/login')
    }
  }
  next()
})

export default router
